﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using App.Data;
using App.Core.Extensions;
using System.Data;


public partial class Pages_Private_Contacts : BasePage
{
    private bool _IsDeleteMode = false;
    private int _ID;

    protected void Page_Load(object sender, EventArgs e)
    {
        BindPageInfo();
        if (!IsPostBack)
        {
            CheckAndDelete();
            BindContactList();
        }
    }
    protected void BindPageInfo()
    {
        _ID = WebUtil.GetQueryStringInInt(AppConstants.QueryString.ID);
        if(String.Compare(WebUtil.GetQueryStringInString(AppConstants.QueryString.DELETE), "True", false) == 0)
            _IsDeleteMode = true;

        Page.Title = WebUtil.GetPageTitle(h4Heading.InnerHtml);
    }
    protected void CheckAndDelete()
    {
        if (_IsDeleteMode)
        {
            BtcHousingEntities context = new BtcHousingEntities();
            var contact = context.Contacts.FirstOrDefault(P => P.ID == _ID && P.CustomerID == SessionCache.CurrentUser.CustomerID);
            if (contact == null)
                WebUtil.ShowMessageBox(divMessage, "Sorry! Requested contact was not found for delete.", true);
            else
            {
                contact.IsDeleted = true;
                context.SaveChanges();
                WebUtil.ShowMessageBox(divMessage, "Contact deleted Successfully.", false);
            }
        }
    }
    protected void BindContactList()
    {
        BtcHousingEntities context = new BtcHousingEntities();        
        var contacts = from C in context.Contacts where C.CustomerID == SessionCache.CurrentUser.CustomerID select C;

        App.Data.DbParameter[] parameters = new[] { new DbParameter("@CustomerID", SessionCache.CurrentUser.CustomerID) };
        UtilityDAO dao = new UtilityDAO();
        int totalRecord = 0;
        DataSet ds = dao.GetPagedData(AppSQL.GET_PAGED_ACTIVE_CONTACTS, parameters, 1, 12, out totalRecord);
        ucContactList.DataSource = ds.Tables[0];
        ucContactList.EditLink = AppConstants.Pages.CONTACT_CHANGE + "?" + AppConstants.QueryString.ID + "={0}";
        ucContactList.DeleteLink = AppConstants.Pages.CONTACT_LIST + "?" + AppConstants.QueryString.ID + "={0}&" + AppConstants.QueryString.DELETE + "=True" ;        
        ucContactList.DeleteMessage = "Are you sure you want to delete this contact?";
        ucContactList.NoRecordMessgae = "No contacts found.";
        ucContactList.DataBind();
    }
}